// pages/songs/songs.js
import PubSub from 'pubsub-js';
import {
  getRecommendSongs,
} from '../../api/index'

Page({

  data: {
    day: '',
    month: '',
    songsList: [],
    index: 0
  },

  onLoad(options) {
    this.getRecommendSongs()
  },

  async getRecommendSongs() {
    const { data: data } = await getRecommendSongs()
    const { dailySongs } = data
    this.setData({ songsList: dailySongs })
    PubSub.subscribe('switchType', (msg, type) => {
      let { songsList, index } = this.data;
      if (type === 'pre') {
        index === 0 && (index = songsList.length);
        index -= 1;
      }else {
        index === songsList.length - 1 && (index = -1);
        index += 1;
      }
      const musicId = songsList[index].id;
      this.setData({ index });
      PubSub.publish('musicId', musicId);
    })
  },

  async onHandleSongItemTap(event) {
    const { id,index } = event.currentTarget.dataset
    this.setData({ index });
    wx.navigateTo({
      url: `/pages/songIndex/songIndex?id=${id}&title=${'每日推荐'}`,
    })
  }

})